package com.jx.flowershopmanager.home.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.google.gson.Gson;
import com.jx.flowershopmanager.App;
import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.adapter.MyGdAdapter;
import com.jx.flowershopmanager.api.FlowerService;
import com.jx.flowershopmanager.base.BaseFragment;
import com.jx.flowershopmanager.entity.FlowersEntity;
import com.jx.flowershopmanager.entity.MessageBean;
import com.jx.flowershopmanager.model.Flowers;
import com.jx.flowershopmanager.network.RetrofitCallback;
import com.jx.flowershopmanager.network.RetrofitUtil;
import com.jx.flowershopmanager.widget.CustomizeMarqueeView;
import com.jx.flowershopmanager.widget.MarquessViewAdapter;
import com.ljx.searchbox.SearchFragment;
import com.ljx.searchbox.custom.IOnSearchClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexFragment extends BaseFragment implements IOnSearchClickListener {

    /**
     * 用户名
     */
    private TextView userText;
    /**
     * 跑马灯View
     */
    private CustomizeMarqueeView mMarqueeView;
    /**
     * 跑马灯适配器
     */
    private MarquessViewAdapter marquessViewAdapter;
    /**
     * 跑马灯数据集合
     */
    private List<MessageBean> messageBeans = new ArrayList<>();
    /**
     * 商品数据集
     */
//    private ArrayList<ProductEntity> productEntityList = null;
    /**
     * grildView
     */
    private GridView gridView;

    /**
     * GridView 适配器
     */
    private MyGdAdapter myGdAdapter;

    /**
     * 标题栏
     */
    private Toolbar toolbar;
    /**
     * 搜索按钮
     */
    private ImageView searchImage;

    /**
     * 后台鲜花实体类
     */
    private List<Flowers.Data> flowersList = new ArrayList<>();
    private SearchFragment searchFragment;

    @Override
    protected int layoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void initView(View view) {
        userText = view.findViewById(R.id.user_name);
        mMarqueeView = view.findViewById(R.id.marqueeView);
        gridView = view.findViewById(R.id.gridview_layout);
        marquessViewAdapter = new MarquessViewAdapter(requireContext());
        mMarqueeView.setItemCount(2);
        mMarqueeView.setSingleLine(false);
        mMarqueeView.setAdapter(marquessViewAdapter);
        marquessViewAdapter.setMessageBeans(messageBeans);
        toolbar = view.findViewById(R.id.toolbar);
        searchImage = view.findViewById(R.id.search_badge);
        searchFragment = SearchFragment.newInstance();
        searchFragment.setOnSearchClickListener(this);
    }

    @Override
    protected void initData(Context context) {
        userText.setText(App.getAppUser().name);
        queryFlower();
//        databaseHelper = new MyDatabaseHelper(context,"FlowerDb.db",null,1);
        /// 添加跑马灯模拟数据
        messageBeans.add(new MessageBean("09:12", "王大翠", "加急", "订购了西兰花"));
        messageBeans.add(new MessageBean("09:12", "大新哥", "一般", "订购了兰花"));
        messageBeans.add(new MessageBean("09:12", "叶凡", "一般", "订购了玫瑰花"));
        messageBeans.add(new MessageBean("09:12", "杨红", "加急", "订购了梅花"));
        /// 刷新适配器
        marquessViewAdapter.notifyDataChanged();

//        queryDbFlower();

        searchImage

                .setOnClickListener(v -> {
            searchFragment.showFragment(requireActivity().getSupportFragmentManager(), SearchFragment.TAG);
        });
    }


    /**
     * 读取数据库数据
     *
     * 不用到这个读取本地数据库的方法，操作sqlite 不够灵活
     */
//    @SuppressLint("Range")
//    private void queryDbFlower() {
//        SQLiteDatabase db = App.getDbHelp().getWritableDatabase();
//        Cursor cursor = db.query("Flowers", null, null, null, null, null, null);
//        List<FlowersEntity> flowersEntityList = new ArrayList<>();
//        if (cursor.moveToFirst()) {
//            do {
//                Integer id = cursor.getInt(cursor.getColumnIndex("id"));
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                Integer category = cursor.getInt(cursor.getColumnIndex("category"));
//                String origin = cursor.getString(cursor.getColumnIndex("origin"));
//                String url = cursor.getString(cursor.getColumnIndex("url"));
//                Integer stock = cursor.getInt(cursor.getColumnIndex("stock"));
//                String describe = cursor.getString(cursor.getColumnIndex("describe"));
//                String createAt = cursor.getString(cursor.getColumnIndex("createAt"));
//                double price = cursor.getDouble(cursor.getColumnIndex("price"));
//                flowersEntityList.add(new FlowersEntity(id, name, category, origin, url, stock, describe, createAt, price));
//            } while (cursor.moveToNext());
//        }
//        myGdAdapter = new MyGdAdapter(flowersEntityList, requireContext());
//        gridView.setAdapter(myGdAdapter);
//        myGdAdapter.notifyDataSetChanged();
//        cursor.close();
//    }

    /**
     * 顶部搜索框，结束输入时候的回调
     * @param keyword 回调回来的值
     */
    @SuppressLint("Range")
    @Override
    public void OnSearchClick(String keyword) {
    }

    private void queryFlower(){
        Map<String, String> map = new HashMap<>(16);

        RetrofitUtil.get("v1/getFlowers", map, new RetrofitCallback() {
            @Override
            public void onSuccess(String resultJsonString) {
                Gson gson = new Gson();
                Flowers flowers = gson.fromJson(resultJsonString,Flowers.class);
                if (flowers.success){
                    flowersList = flowers.getData();
                    myGdAdapter = new MyGdAdapter(flowersList, requireContext());
                    gridView.setAdapter(myGdAdapter);
                    myGdAdapter.notifyDataSetChanged();
                    Log.i("IndexFragment",flowers.getMsg());
                }
            }

            @Override
            public void onError(Throwable t) {
                Log.i("TAG", t.getMessage());

            }
        });
    }
}